package logic

import (
	"apple/cache"
	"apple/models/mysql"
	"context"
	"fmt"

	"apple/applerpc/applerpc"
	"apple/applerpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserAddLogic {
	return &UserAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 注册
func (l *UserAddLogic) UserAdd(in *applerpc.UserAddRequest) (*applerpc.UserAddResponse, error) {
	// todo: add your logic here and delete this line
	user := mysql.User{
		Account:  in.Account,
		Password: in.Password,
		Tel:      in.Tel,
	}
	get := cache.Rdb.Get(l.ctx, "sendsms"+in.Tel)
	if get.Val() != in.SendSms {
		return nil, fmt.Errorf("短信错误")
	}
	mysql.DB.Debug().Where("account=?", in.Account).Find(&user)
	if user.ID != 0 {
		return nil, fmt.Errorf("用户名已存在无法注册")
	}
	mysql.DB.Debug().Create(&user)
	return &applerpc.UserAddResponse{
		Id: int64(user.ID),
	}, nil
}
